<?php
include 'mysql.inc.php';
$phone = $_POST['phone'];
$name = $_POST['name'];

if (!isset($phone) || !isset($name)) {
	exit;
}

$sql = "select * from `user` where `phone`='{$phone}'";
$rs = query($sql);
$row = mysql_fetch_assoc($rs);
$user = -1;
$choice = 3;

$starid =$_COOKIE["vote"];;

if ($row) {
	/*
	$result = array("res" => 'fail', "desc"=>"姓名不正确");
	echo json_encode($result);
	closeConnection();
	exit;
	*/
	
	if ($row['name']==$name) {
		$user = $row['id'];
		$choice = $row['choice'] * 1;
		if ($choice < 1) {
			$result = array("res" => 'exist', "desc"=>"您已经参加过vivo乐享真声音活动，感谢您的参与，谢谢");
			echo json_encode($result);
			closeConnection();
			exit;
		}
	} else {
		$result = array("res" => 'fail', "desc"=>"姓名不正确");
		echo json_encode($result);
		closeConnection();
		exit;
	}
} else {
	
	if (!isset($starid)) {
		$starid = 1;
	}
	$sql = "select * from `star` where `id`='{$starid}'";
	$rs = query($sql);
	$row = fetch_one($rs);
	
	if (!$row) {
		$starid = 1;
	}
	$sql = "INSERT IGNORE INTO `user`(`phone`, `name`, `create_time`, `starid`, `choice`, `rank`) VALUES('{$phone}', '{$name}', NOW(), '{$starid}', 3, 0)";
	query($sql);
	$user = mysql_insert_id();
}
session_start();
$_SESSION['user'] = $user;

$result = array("res" => 'success', "choice" => $choice, "vote" => $starid);
echo json_encode($result);
closeConnection();
?>